﻿Public Class QuanLyChiBUS
    Shared Function GetAll() As DataTable
        Return QuanLyChiDAO.GetAll()
    End Function
    Shared Function GetAllByMa(ByVal MaPhieuChi As Integer) As DataTable
        Return QuanLyChiDAO.GetAllByMa(MaPhieuChi)
    End Function
    Shared Function TrungBinh(ByVal MaUser As Integer) As Double
        Return QuanLyChiDAO.TrungBinh(MaUser)
    End Function
    Shared Function Write(obj As PhieuChi, Optional MaPhieuChi As Integer = 0) As Boolean
        If obj.MaUserChi.Equals(0) Then
            MessageBox.Show("Xin chọn người dùng")
            Return False
        End If
        If obj.MaNganHang.Equals(0) Then
            MessageBox.Show("Xin chọn tài khoản")
            Return False
        End If
        If obj.MaChiPhi.Equals(0) Then
            MessageBox.Show("Xin chọn loại thu nhập")
        End If
        Dim dr As DataRow
        Dim dt As DataTable
        If MaPhieuChi = 0 Then
            dt = DB_Provider.Read_schema("PhieuChi")
            dr = dt.NewRow()
            dr("Ngay") = obj.Ngay
            dr("MaNganHang") = obj.MaNganHang
            dr("MaUserChi") = obj.MaUserChi
            dr("MaUserLap") = obj.MaUserLap
            dr("SoTien") = obj.SoTien
            dr("MaChiPhi") = obj.MaChiPhi
            dr("ChiTiet") = obj.ChiTiet
            dt.Rows.Add(dr)
        Else
            dt = GetAllByMa(MaPhieuChi)
            Dim pk(1) As DataColumn
            pk(0) = dt.Columns("MaPhieuChi")
            dt.PrimaryKey = pk
            dr = dt.Rows.Find(MaPhieuChi)
            dr("Ngay") = obj.Ngay
            dr("MaNganHang") = obj.MaNganHang
            dr("MaUserChi") = obj.MaUserChi
            dr("MaUserLap") = obj.MaUserLap
            dr("SoTien") = obj.SoTien
            dr("MaChiPhi") = obj.MaChiPhi
            dr("ChiTiet") = obj.ChiTiet
        End If
        DB_Provider.Write(dt, "PhieuChi")
        Dim MaNganHang As Integer = Integer.Parse(dt.Rows(0).Item("MaNganHang"))
        Dim SoTien As Double = Double.Parse(dt.Rows(0).Item("SoTien").ToString())
        TaiKhoanNganHangDAO.UpdateSoTien(MaNganHang, SoTien, False)
        Return True
    End Function
End Class
